#!/bin/bash

cd /kyo/group/

randFile() {
    local line
    while read line ; do
        echo ${RANDOM}_$line
    done < $1 | sort -n | sed -nr 's/^[0-9]+_//p'
}

randStdin() {
    local line
    while read line ; do
        echo ${RANDOM}_$line
    done | sort -n | sed -nr 's/^[0-9]+_//p'
}

randItem() {
    local line item
    while read line ; do
        item=$(randFile item.txt | tail -n 1)
        echo "$line --> $item"
    done
}

randGroupItem() {
    while : ; do
        tail -n 10 grouplist.txt | randStdin | randItem > groupitem.txt
        num=$(awk '{print $NF}' groupitem.txt | sort | uniq | wc -l)
        test $num -eq 9 && break
        # if test $num -ge 8 ; then
            # echo -e "\033[31;1m$num\033[0m:"
            # cat groupitem.txt
            # read -n 1 input
            # test "x$input" = "xq" && break
        # fi
    done
}


randNum=$(awk -F":" '{print $1}' list.txt | sort -n | head -n 1)
name=$(grep "$randNum" list.txt | randStdin | head -n 1 | awk -F":" '{print $2}')
echo $name
read -n 1 -p "是否保存(Y/n): " input
if test "x$input" = "xy" -o "x$input" = "xY" ; then
    let randNum++
    sed -i -r "s/^[0-9]+(:$name)$/$randNum\1/" list.txt
    echo "$(date +%F) $name" >> histroy.txt
fi
echo
